package com.atguigu.gmall.oms.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.atguigu.gmall.oms.entity.OrderItemEntity;
import com.atguigu.gmall.oms.vo.OrderItemVo;
import com.atguigu.gmall.oms.vo.OrderSubmitVo;
import com.google.gson.Gson;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.service.OrderService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    Gson gson = new Gson();

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<OrderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<OrderEntity>()
        );

        return new PageResultVo(page);
    }


    /**
     * 保存订单的数据库层操作方法。
     * 需要整合分布式事务，因为这个订单保存的访问量肯定非常大，为了避免并发太大，这个方法处理不过来，
     * 我们会以多实例的方式启动，启动之后怎么保证复杂的业务同时成功同时失败呢，我们需要使用分布式事务的管理
     *
     * @param orderSubmitVo
     * @param userId
     * @return
     */
    @Transactional//本地事务，不是声明式事务，加上之后，下面方法里所有的方法调用，要么一起成功，要么一起失败
    @Override
    public OrderEntity saveOrder(OrderSubmitVo orderSubmitVo, Long userId) {
        //保存订单对象到数据库表
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setUserId(userId);
        orderEntity.setStatus(0);//表示订单状态为待支付
        orderEntity.setCreateTime(new Date());
        orderEntity.setPayAmount(orderSubmitVo.getTotalPrice());//应付金额=总金额-积分抵扣金额，我们暂定积分抵扣金额是0
        orderEntity.setTotalAmount(orderSubmitVo.getTotalPrice());//总金额
        orderEntity.setPayType(1);//默认支付宝支付
        orderEntity.setOrderSn(orderSubmitVo.getOrderToken());//订单编号
        orderEntity.setDeliveryCompany(orderSubmitVo.getDeliveryCompany());
        orderEntity.setReceiverName(orderSubmitVo.getAddress().getAddress());
        orderEntity.setReceiverCity(orderSubmitVo.getAddress().getCity());
        orderEntity.setReceiverPhone(orderSubmitVo.getAddress().getPhone());
        orderEntity.setGrowth(orderSubmitVo.getTotalPrice().intValue());//计算购买商品sku 得到 总的成长积分
        orderEntity.setIntegration(orderSubmitVo.getTotalPrice().intValue());//计算可以获得的积分
        //发票，暂时不写了
        //优惠券的使用，我们暂时不写了

        //保存订单项到数据库表
        List<OrderItemVo> orderItemVos = orderSubmitVo.getItems();
        if (CollectionUtils.isNotEmpty(orderItemVos)){
            orderItemVos.stream().map(orderItemVo -> {
                OrderItemEntity orderItemEntity = new OrderItemEntity();
                orderItemEntity.setOrderId(orderEntity.getId());
                orderItemEntity.setOrderSn(orderEntity.getOrderSn());
                orderItemEntity.setSkuQuantity(orderItemVo.getCount());//购买数量
                orderItemEntity.setSkuName(orderItemVo.getTitle());
                orderItemEntity.setSkuPic(orderItemVo.getDefaultImage());
                orderItemEntity.setSkuId(Long.parseLong(orderItemVo.getSkuId()));
                orderItemEntity.setSkuPrice(orderItemVo.getPrice());
                orderItemEntity.setSkuAttrsVals(gson.toJson(orderItemVo.getSaleAttrs()));
                orderItemEntity.setRealAmount(orderItemVo.getPrice());

                return orderItemEntity;
            }).collect(Collectors.toList());
        }

        return null;
    }

}